﻿using System;
using System.Collections;
using System.Collections.Generic;
using Wintellect.PowerCollections;

namespace ComplexNumbers
{
    class PriorityQueue<T> : IEnumerable<T>
    {
        private OrderedBag<T> collection;

        public PriorityQueue()
        {
            this.collection = new OrderedBag<T>();
        }

        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < collection.Count; i++)
            {
                yield return collection[i];
            }
            //return collection.GetEnumerator();
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return this.GetEnumerator() as IEnumerator<T>;
        }

        public void Enqueue(T data)
        {
            if (data == null)
            {
                throw new NullReferenceException("Data not initialized");
            }
            this.collection.Add(data);
        }

        public T Dequeue()
        {
            if (this.collection.Count == 0)
            {
                throw new MissingMemberException("No members in collection");
            }

            T data = collection[0];
            this.collection.Remove(data);

            return data;
        }

        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= this.collection.Count)
                {
                    throw new IndexOutOfRangeException("Index was out of the bounds of the queue");
                }

                return this.collection[index];
            }
        }
    }
}
